import numpy as np
from numpy import *
import matplotlib.pylab as plt

from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']

# 定义训练集
X = np.array([
    [1, 1, 1],
    [1, 0, 0],
    [1, 0, 1],
    [1, 1, 0],
])
y = np.array([
    [1],
    [1],
    [0],
    [0],
])

# 定义sigmoid函数及偏导
def sigmoid(x, deriv=False):
    if deriv:
        return x * (1 - x)
    return 1.0 / (1.0 + np.exp(-x))

# 初始化theta值，均值为0
theta1 = 2 * np.random.random((3, 3)) - 1
theta2 = 2 * np.random.random((3, 1)) - 1

# 初始化代价函数
J_history = np.zeros(15000)
m = X.shape[0]
alpha = 0.3

for i in range(15000):
    # 前向传播算法
    a1 = X
    a2 = sigmoid(a1.dot(theta1))
    a3 = sigmoid(a2.dot(theta2))

    # 记录代价函数
    J_history[i] = -1.0 / m * (y.T.dot(np.log(a3)) + (1 - y).T.dot(np.log(1 - a3)))

    # 反向传播算法
    delta3 = a3 - y
    delta2 = delta3.dot(theta2.T) * sigmoid(a2, True)

    deltheta2 = (1 / m) * a2.T.dot(delta3)
    deltheta1 = (1 / m) * a1.T.dot(delta2)

    theta2 -= alpha * deltheta2
    theta1 -= alpha * deltheta1

plt.plot(J_history)
plt.show()
